package com.rc.web.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.rc.common.util.RabbitMqHelper;
import com.rc.pojo.dto.consumer.ConsumerDTO;
import com.rc.pojo.dto.consumer.ConsumerLoginDTO;
import com.rc.pojo.po.Consumer;
import com.rc.pojo.vo.consumer.ConsumerLoginVO;
import com.rc.pojo.vo.consumer.ConsumerVO;
import com.rc.web.converter.ConsumerConverter;
import com.rc.web.service.ConsumerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 移动端游客用户表 控制层。
 *
 * @author sym
 * @since 2024-05-24
 */
@RestController
@RequestMapping("/consumer")
@Api(tags = "顾客")
@RequiredArgsConstructor
public class ConsumerController {
    private final ConsumerService consumerService;
    private final ConsumerConverter consumerConverter;
    private final RabbitMqHelper rabbitMqHelper;

    @ApiOperation("游客登录")
    @PostMapping("/login")
    public ConsumerLoginVO login(@Validated @RequestBody ConsumerLoginDTO dto){
        return consumerService.login(dto.getCode());
    }

    @ApiOperation("顾客获取自己信息")
    @GetMapping("/info")
    public ConsumerVO getInfo(){
        return consumerService.getInfo();
    }


    @PutMapping
    @ApiOperation("顾客更新信息")
    public void updateById(@RequestBody ConsumerDTO dto){
        Consumer consumer = consumerConverter.dto2DO(dto);
        long conId = StpUtil.getLoginIdAsLong();
        consumer.setId(conId);
        consumerService.updateById(consumer);

        StpUtil.getSession().set("consumer",consumerService.getById(conId));
        //发布事件更新评论中该用户的所有评论的头像和昵称
        rabbitMqHelper.sendMessage("user.direct","update",conId);
    }
}
